package com.zhuanzhuan.hero.user.facade;

import com.bj58.spat.scf.server.contract.annotation.ServiceBehavior;
import com.bj58.zhuanzhuan.zzentry.common.service.entity.ServiceHeaderEntity;
import com.bj58.zhuanzhuan.zzentry.common.service.entity.ZZOpenScfBaseResult;
import com.bj58.zhuanzhuan.zzentry.common.service.model.openEntry.ZZHeader;
import com.bj58.zhuanzhuan.zzentry.common.service.model.openEntry.ZZMethod;
import com.bj58.zhuanzhuan.zzentry.common.service.model.openEntry.ZZParam;
import com.zhuanzhuan.hero.member.enums.AuthCodeEnum;
import com.zhuanzhuan.hero.user.consts.NumConsts;
import com.zhuanzhuan.hero.user.dto.QuerySetupOptionDTO;
import com.zhuanzhuan.hero.user.enums.SetupOptionEnum;
import com.zhuanzhuan.hero.user.helper.HeroMemberHelper;
import com.zhuanzhuan.hero.user.service.HeroUserContactService;
import com.zhuanzhuan.hero.user.service.HeroUserInfoService;
import com.zhuanzhuan.hero.user.service.HeroUserLabelRelationService;
import com.zhuanzhuan.hero.user.service.IApolloService;
import com.zhuanzhuan.hero.user.util.ServiceHeaderUtil;
import com.zhuanzhuan.hero.user.vo.setup.SettingItemVO;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @ClassName SetupFacadeImpl
 * @Description
 * @Author hanjunjun
 * @Date 2024/7/11 17:15
 */
@ServiceBehavior
@Slf4j
public class SetupFacadeImpl implements ISetupFacade {

    @Resource
    private IApolloService apolloService;

    @Resource
    private HeroUserInfoService userInfoService;

    @Resource
    private HeroUserContactService userContactService;

    @Resource
    private HeroUserLabelRelationService labelRelationService;

    @Resource
    private HeroMemberHelper heroMemberHelper;

    @ZZMethod(value = "querySetupOptions", desc = "设置选项下发", publicAccess = false)
    @Override
    public ZZOpenScfBaseResult<List<SettingItemVO>> querySetupOptions(@ZZHeader ServiceHeaderEntity header,
        @ZZParam("param") @Valid QuerySetupOptionDTO querySetupOptionDTO) {
        Long uid = ServiceHeaderUtil.parseUid(header);
        List<SettingItemVO> voList = apolloService.querySetupOptions(querySetupOptionDTO.getSourceType());
        //判断是否填写完成
        voList.forEach(vo -> {
            String itemCode = vo.getItemCode();
            if (SetupOptionEnum.BASE_INFO.getCode().equals(itemCode)) {
                vo.setCompleted(userInfoService.baseInfoIsPerfection(uid, null));
            } else if (SetupOptionEnum.CONTACT_INFO.getCode().equals(itemCode)) {
                vo.setCompleted(userContactService.contactInfoIsPerfection(uid, null));
            } else if (SetupOptionEnum.MANAGEMENT_LABEL.getCode().equals(itemCode)) {
                vo.setCompleted(labelRelationService.managementLabelIsPerfection(uid, null));
            }
        });

        //分享管理只有高级会员才展示  如果不是高级会员则移除
        if (NumConsts.TWO.equals(querySetupOptionDTO.getSourceType())) {
            if (!heroMemberHelper.hasAuth(uid, AuthCodeEnum.SHARE_MANAGE.getCode())) {
                voList.removeIf(vo -> SetupOptionEnum.SHARE.getCode().equals(vo.getItemCode()));
            }
        }

        return ZZOpenScfBaseResult.buildSucc(voList);
    }
}
